require 'rake'
require 'yaml'

# Load the AutumnLeaves environment.
task :environment do
  # Load the global settings
  root = File.dirname(__FILE__)
  begin
    $AL_ENV = YAML.load(File.open("#{root}/config/global.yml"))
  rescue SystemCallError
    raise "Couldn't find your global.yml file."
  end
  $AL_ENV['root'] = File.expand_path(root)

  # Load the season
  season_dir = "#{root}/config/seasons/#{$AL_ENV['season']}"
  begin
    raise "No leaves.yml file for the current season." unless Dir.entries(season_dir).include? 'leaves.yml'
  rescue SystemCallError
    raise "The current season doesn't have a directory."
  end
  begin
    $AL_ENV['season_config'] = YAML.load(File.open("#{season_dir}/season.yml"))
  rescue
    # season.yml is optional
  end
  
  # Load the leaf
  leaf_info = YAML.load(File.open("#{season_dir}/leaves.yml"))
  leaf_info.each do |leaf|
    $AL_ENV['leaf'] ||= leaf['nick']
    $AL_ENV['leaf_type'] ||= leaf['type']
  end

  # Load the data file
  $AL_ENV['data_file'] = "#{$AL_ENV['root']}/data/#{$AL_ENV['leaf']}.#{$AL_ENV['season']}.yml"
end

namespace :app do
  desc "Start an instance of the application"
  task :start do
    system("#{File.dirname(__FILE__)}/big-switch.rb start")
  end
  
  desc "Stop all instances of the application"
  task :stop do
    system("#{File.dirname(__FILE__)}/big-switch.rb stop")
  end
  
  desc "Stop all instances and restart them afterwards"
  task :restart do
    system("#{File.dirname(__FILE__)}/big-switch.rb restart")
  end
  
  desc "Start the application and stay on top"
  task :run do
    system("#{File.dirname(__FILE__)}/big-switch.rb run")
  end
  
  desc "Forces the app to a stopped state"
  task :zap do
    system("#{File.dirname(__FILE__)}/big-switch.rb zap")
  end
end

namespace :log do
  desc "Remove any log files"
  task :clear do
    system("rm -vf autumn-leaves.log autumn-leaves.output log/*.log")
  end

  desc "Check the logs for any errors"
  task :errors => :environment do
    log_file = "#{$AL_ENV['root']}/log/#{$AL_ENV['season']}.log"
    al_output = "#{$AL_ENV['root']}/autumn-leaves.output"
    if File.exists?(log_file)
      puts "==== ERROR-LEVEL LOG MESSAGES ===="
      File.open(log_file, 'r') do |log|
        puts log.grep(/Error/)
      end
    end
    if File.exists?(al_output)
      puts "====   UNCAUGHT EXCEPTIONS    ===="
      File.open(al_output, 'r') do |log|
        puts log.grep(/ERROR/)
      end
    end
  end
end

namespace :data do
  desc "Backup the data file for the current season"
  task :backup => :environment do
    if File.exists?($AL_ENV['data_file'])
      File.copy($AL_ENV['data_file'], "#{$AL_ENV['data_file']}.backup", true)
    end
  end

  desc "Destroy the stored data file for the current season"
  task :destroy => :environment do
    Rake::Task['data:backup'].invoke
    if File.exists?($AL_ENV['data_file'])
      File.delete($AL_ENV['data_file'])
    end
  end
  
  desc "Restore the data file from backup"
  task :restore => :environment do
    if File.exists?("#{$AL_ENV['data_file']}.backup")
      File.move("#{$AL_ENV['data_file']}.backup", $AL_ENV['data_file'], true)
    end
  end
end

namespace :doc do
  desc "Generate API documentation"
  task :app => [:environment, :clear] do
    system("rdoc --main README --title '#{$AL_ENV['leaf_type']} Documentation' -o #{$AL_ENV['root']}/doc/api --line-numbers --inline-source libs leaves README support genesis.rb big-switch.rb")
  end
  
  desc "Remove the API documentation"
  task :clear => :environment do
    if File.directory?("#{$AL_ENV['root']}/doc/api")
      system("rm -rvf #{$AL_ENV['root']}/doc/api")
    end
  end
end
